import { defineStore } from 'pinia'
import { ref } from 'vue'

export interface UserInfo {
  id: number
  username: string
  name: string
  email: string
  password: string 
  phoneNumber: string
  avatarUrl: string
  createTime: string
  updateTime: string
  token: string
}

export const useUserStore = defineStore('user', () => {
  const userInfo = ref<UserInfo | null>(null)

  const setUserInfo = (info: UserInfo) => {
    userInfo.value = info
    localStorage.setItem('userInfo', JSON.stringify(info))
  }

  const getUserInfo = () => {
    if (!userInfo.value) {
      const stored = localStorage.getItem('userInfo')
      if (stored) {
        userInfo.value = JSON.parse(stored)
      }
    }
    return userInfo.value
  }

  const clearUserInfo = () => {
    userInfo.value = null
    localStorage.removeItem('userInfo')
  }

  return {
    userInfo,
    setUserInfo,
    getUserInfo,
    clearUserInfo
  }
})
